package cn.edu.tju.elm.model.wallet;

import cn.edu.tju.core.model.BaseEntity;
import jakarta.persistence.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 表示用户的钱包，包含余额、冻结金额、透支额度和奖励金额等信息。
 * 字段说明：
 * id            - 钱包唯一标识
 * userId        - 钱包所属用户的ID
 * balance       - 当前可用余额
 * frozenAmount  - 当前被冻结且不可用的金额
 * overdraftLimit- 钱包允许的最大透支额度
 * rewardAmount  - 钱包中的奖励金额
 * membershipLevel - 会员级别（REGULAR/SVIP）
 * membershipExpireTime - 会员过期时间
 */

@Entity
@Table(name = "wallet")
public class Wallet extends BaseEntity {
	
	@Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    @Column(name = "user_id", nullable = false)
    private Long userId;
    
    @Column(name = "balance", precision = 15, scale = 2)
    private BigDecimal balance;
    
    @Column(name = "frozen_amount", precision = 15, scale = 2)
    private BigDecimal frozenAmount;
    
    @Column(name = "overdraft_limit", precision = 15, scale = 2)
    private BigDecimal overdraftLimit;
    
    @Column(name = "reward_amount", precision = 15, scale = 2)
    private BigDecimal rewardAmount;

    @Column(name = "incoming_frozen_amount", precision = 15, scale = 2)
    private BigDecimal incomingFrozenAmount;

    @Enumerated(EnumType.STRING)
    @Column(name = "membership_level", length = 20)
    private MembershipLevel membershipLevel = MembershipLevel.REGULAR;

    @Column(name = "membership_expire_time")
    private LocalDateTime membershipExpireTime;
    
    // Getters and Setters
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public BigDecimal getBalance() {
        return balance;
    }

    public void setBalance(BigDecimal balance) {
        this.balance = balance;
    }

    public BigDecimal getFrozenAmount() {
        return frozenAmount;
    }

    public void setFrozenAmount(BigDecimal frozenAmount) {
        this.frozenAmount = frozenAmount;
    }

    public BigDecimal getOverdraftLimit() {
        return overdraftLimit;
    }

    public void setOverdraftLimit(BigDecimal overdraftLimit) {
        this.overdraftLimit = overdraftLimit;
    }

    public BigDecimal getRewardAmount() {
        return rewardAmount;
    }

    public void setRewardAmount(BigDecimal rewardAmount) {
        this.rewardAmount = rewardAmount;
    }

    public BigDecimal getIncomingFrozenAmount() {
        return incomingFrozenAmount;
    }

    public void setIncomingFrozenAmount(BigDecimal incomingFrozenAmount) {
        this.incomingFrozenAmount = incomingFrozenAmount;
    }

    public MembershipLevel getMembershipLevel() {
        return membershipLevel;
    }

    public void setMembershipLevel(MembershipLevel membershipLevel) {
        this.membershipLevel = membershipLevel;
    }

    public LocalDateTime getMembershipExpireTime() {
        return membershipExpireTime;
    }

    public void setMembershipExpireTime(LocalDateTime membershipExpireTime) {
        this.membershipExpireTime = membershipExpireTime;
    }

    /**
     * 判断是否为有效的SVIP会员
     */
    public boolean isActiveSvip() {
        return membershipLevel == MembershipLevel.SVIP 
            && membershipExpireTime != null 
            && membershipExpireTime.isAfter(LocalDateTime.now());
    }

}
